#include <iostream>
#include <queue>
#include <deque>
int nums[(int)1e6 + 10];
using namespace std;
// 思路是保持区间的单调性,就不用每次都遍历区间
// 存储下标方便判断是否在区间里
// 用deque 或list 提高效率
#include <iostream>
#include <queue>
#include <deque>
int nums[(int)1e6 + 10];
using namespace std;
// 思路清晰版
int main()
{
    deque<int> deq;
    int m;
    int n;
    cin >> m >> n;
    int l = 0;
    for (int i = 0; i < m; i++)
    {
        cin >> nums[i];
    }
    // 小
    // 形成区间
    for (int i = 0; i < n; i++)
    {
        while (deq.size() && nums[i] <= nums[deq.back()])
        {
            deq.pop_back();
        }
        deq.push_back(i);
    }
    cout << nums[deq.front()] << ' ';
    // 滑动区间 + 单调性
    for (int i = n; i < m; i++)
    {
        l++;
        if (deq.front() < l)
            deq.pop_front();
        while (deq.size() && nums[i] <= nums[deq.back()])
        {
            deq.pop_back();
        }
        deq.push_back(i);
        cout << nums[deq.front()] << ' ';
    }
    // 大
    deq.clear();
    l = 0;
    cout << endl;
    for (int i = 0; i < n; i++)
    {
        while (deq.size() && nums[i] >= nums[deq.back()])
        {
            deq.pop_back();
        }
        deq.push_back(i);
    }
    cout << nums[deq.front()] << ' ';
    for (int i = n; i < m; i++)
    {
        l++;
        if (deq.front() < l)
            deq.pop_front();
        while (deq.size() && nums[i] >= nums[deq.back()])
        {
            deq.pop_back();
        }
        deq.push_back(i);
        cout << nums[deq.front()] << ' ';
    }
    return 0;
}
// 代码简化版
int main()
{
    deque<int> deq;
    int m;
    int n;
    cin >> m >> n;
    for (int i = 0; i < m; i++)
    {
        cin >> nums[i];
    }
    for (int i = 0; i < m; i++)
    {
        // 假设区间为[-n,-1]
        if (deq.front() < i - n + 1)
            deq.pop_front();
        while (deq.size() && nums[i] <= nums[deq.back()])
        {
            deq.pop_back();
        }
        deq.push_back(i);
        if (i >= n - 1)
            cout << nums[deq.front()] << ' ';
    }
    deq.clear();
    cout << endl;
    for (int i = 0; i < m; i++)
    {
        if (deq.front() < i - n + 1)
            deq.pop_front();
        while (deq.size() && nums[i] >= nums[deq.back()])
        {
            deq.pop_back();
        }
        deq.push_back(i);
        if (i >= n - 1)
            cout << nums[deq.front()] << ' ';
    }
    return 0;
}